package org.example.service;

import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.common.stream.StreamObserver;
import org.example.User;
import org.example.UserRequest;
import org.example.UserService;
import org.example.bean.Store;
import org.springframework.stereotype.Service;

@Service
public class OrderService {

    @DubboReference
    private UserService userService;

    @DubboReference
    private StoreService storeService;

    public User getUser() {
        System.out.println("客户端===用户(getUser)===调用远程服务");
        User user = userService.getUser(UserRequest.newBuilder().setUid("123").build());
        System.out.println("收到数据 -> " + user + "\n");
        return user;
    }

    public void getUserStream() {
        System.out.println("客户端===用户(getUserStream)===调用远程服务");
        StreamObserver<UserRequest> streamObserver = userService.getUserStream(new StreamObserver<User>() {
            @Override
            public void onNext(User user) {
                System.out.print("客户端===用户(getUserStream)===onNext===接受到响应数据 -> " + user);
            }

            @Override
            public void onError(Throwable throwable) {
                System.out.println("客户端===用户(getUserStream)===Throwable -> " + throwable);
            }

            @Override
            public void onCompleted() {
                System.out.println("客户端===用户(getUserStream)===接收到响应数据完毕\n");
            }
        });

        // 发送数据到服务端
        System.out.print("客户端===用户(getUserStream)===发送数据到服务端 -> " + UserRequest.newBuilder().setUid("123").build());
        streamObserver.onNext(UserRequest.newBuilder().setUid("123").build());
        streamObserver.onCompleted();
    }


    public Store getStore() {
        System.out.println("客户端===商店(getStore)===调用远程服务");
        Store store = storeService.getStore("123");
        System.out.println("收到数据 -> " + store + "\n");
        return store;
    }

    public void getStoreServerStream() {
        System.out.println("客户端===商店(getStoresServerStream)===调用远程服务");
        storeService.getStoresServerStream("123", new StreamObserver<Store>() {
            @Override
            public void onNext(Store store) {
                System.out.println("客户端===商店(getStoresServerStream)===onNext===服务端返回的数据 -> " + store);
            }

            @Override
            public void onError(Throwable throwable) {
                System.out.println("客户端===商店(getStoresServerStream)===Throwable -> " + throwable);
            }

            @Override
            public void onCompleted() {
                System.out.println("客户端===商店(getStoresServerStream)===服务端返回数据完毕\n");
            }
        });
    }

    public void getStoreClientStream() {
        System.out.println("客户端===商店(getStoreClientStream)===调用远程服务");
        StreamObserver<Store> streamObserver = storeService.getStoresClientStream(new StreamObserver<Store>() {
            @Override
            public void onNext(Store store) {
                System.out.println("客户端===商店(getStoreClientStream)===onNext===接受到响应数据 -> " + store);
            }

            @Override
            public void onError(Throwable throwable) {
                System.out.println("客户端===商店(getStoreClientStream)===Throwable -> " + throwable);
            }

            @Override
            public void onCompleted() {
                System.out.println("客户端===商店(getStoreClientStream)===接收到响应数据完毕\n");
            }
        });

        // 发送数据到服务端
        Store store = Store.builder().build().setStoreId("123").setStoreName("chenwj");
        streamObserver.onNext(store);
        streamObserver.onCompleted();
    }

}
